//
//  KATLabel.m
//  KATFramework
//
//  Created by Kantice on 16/1/26.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATLabel.h"




@interface KATLabel ()

///标签控件
@property(nonatomic,retain) UILabel *label;

///属性字符串
@property(nonatomic,retain) NSMutableAttributedString *attrString;

///内容尺寸
@property(nonatomic,assign) CGSize contentSize;

///文本尺寸
@property(nonatomic,assign) CGSize textSize;

@end



@implementation KATLabel


+ (instancetype)labelWithFrame:(CGRect)frame andStyle:(KATTextStyle *)style
{
    if(!style)//空值则使用默认的style
    {
        style=[KATTextStyle textStyle];
    }
    
    KATLabel *label=[[[self alloc] init] autorelease];
        
    label.frame=frame;
    
    label.label=[[[UILabel alloc] init] autorelease];
    label.label.backgroundColor=[UIColor clearColor];
    [label addSubview:label.label];
    
    label.attrString=[[[NSMutableAttributedString alloc] init] autorelease];
    
    label.style=style;
    
    return label;
}


//设置内容
- (void)setText:(NSString *)text
{
    if(_text!=text)
    {
        [_text release];
        _text=nil;
    
        _text=[text retain];
    }
    
    [self updateStyle];
}


//设置样式
- (void)setStyle:(KATTextStyle *)style
{
    if(_style!=style)
    {
        [_style release];
        _style=nil;
    
        _style=[style retain];
    }
    
    [self updateStyle];
}


- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    _label.frame=CGRectMake(_style.marginLeft, _style.marginTop, self.frame.size.width-_style.marginLeft-_style.marginRight, self.frame.size.height-_style.marginTop-_style.marginBottom);
}


//更新样式和内容尺寸
- (void)updateStyle
{
    if(_style && _text)
    {
        //计算尺寸
        _label.frame=CGRectMake(_style.marginLeft, _style.marginTop, self.frame.size.width-_style.marginLeft-_style.marginRight, self.frame.size.height-_style.marginTop-_style.marginBottom);
        
        
        //控件设置
        _label.textAlignment=_style.textAlignment;
        _label.lineBreakMode=_style.textLineBreak;
        _label.numberOfLines=_style.lines;
        _label.font=_style.textFont;
        _label.textColor=_style.textColor;
//        self.backgroundColor=_style.bgColor;
        _label.text=_text;
        
        //设置文本
        [_attrString replaceCharactersInRange:NSMakeRange(0, _attrString.length) withString:_text];
        
        
        //设置全局样式
        if(_style.paragraph)//有全局段落样式
        {
            [_attrString setAttributes:@{
                                         NSBackgroundColorAttributeName:_style.textBgColor,
                                         NSForegroundColorAttributeName:_style.textColor,
                                         NSFontAttributeName:_style.textFont,
                                         NSParagraphStyleAttributeName:_style.paragraph
                                         }
                                 range:NSMakeRange(0, _attrString.length)];
        }
        else
        {
            [_attrString setAttributes:@{
                                         NSBackgroundColorAttributeName:_style.textBgColor,
                                         NSForegroundColorAttributeName:_style.textColor,
                                         NSFontAttributeName:_style.textFont
                                         }
                                 range:NSMakeRange(0, _attrString.length)];
        }
        
        
        //表达式样式
        if(_style.hasExpStyle && _style.expression)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:_style.expression inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.expBgColor,
                                                 NSForegroundColorAttributeName:_style.expTextColor,
                                                 NSFontAttributeName:_style.expFont,
                                                 NSStrikethroughColorAttributeName:_style.expLineColor,
                                                 NSUnderlineColorAttributeName:_style.expLineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.expUnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.expDeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //表达式样式2
        if(_style.hasExp2Style && _style.expression2)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:_style.expression2 inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.exp2BgColor,
                                                 NSForegroundColorAttributeName:_style.exp2TextColor,
                                                 NSFontAttributeName:_style.exp2Font,
                                                 NSStrikethroughColorAttributeName:_style.exp2LineColor,
                                                 NSUnderlineColorAttributeName:_style.exp2LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.exp2UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.exp2DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //表达式样式3
        if(_style.hasExp3Style && _style.expression3)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:_style.expression3 inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.exp3BgColor,
                                                 NSForegroundColorAttributeName:_style.exp3TextColor,
                                                 NSFontAttributeName:_style.exp3Font,
                                                 NSStrikethroughColorAttributeName:_style.exp3LineColor,
                                                 NSUnderlineColorAttributeName:_style.exp3LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.exp3UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.exp3DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式
        if(_style.hasKeywordsStyle && _style.keywords && _style.keywords.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywordsBgColor,
                                                 NSForegroundColorAttributeName:_style.keywordsTextColor,
                                                 NSFontAttributeName:_style.keywordsFont,
                                                 NSStrikethroughColorAttributeName:_style.keywordsLineColor,
                                                 NSUnderlineColorAttributeName:_style.keywordsLineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywordsUnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywordsDeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式2
        if(_style.hasKeywords2Style && _style.keywords2 && _style.keywords2.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords2] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords2BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords2TextColor,
                                                 NSFontAttributeName:_style.keywords2Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords2LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords2LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords2UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords2DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }

        
        
        //关键字样式3
        if(_style.hasKeywords3Style && _style.keywords3 && _style.keywords3.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords3] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords3BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords3TextColor,
                                                 NSFontAttributeName:_style.keywords3Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords3LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords3LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords3UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords3DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式4
        if(_style.hasKeywords4Style && _style.keywords4 && _style.keywords4.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords4] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords4BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords4TextColor,
                                                 NSFontAttributeName:_style.keywords4Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords4LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords4LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords4UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords4DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式5
        if(_style.hasKeywords5Style && _style.keywords5 && _style.keywords5.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords5] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords5BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords5TextColor,
                                                 NSFontAttributeName:_style.keywords5Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords5LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords5LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords5UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords5DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式6
        if(_style.hasKeywords6Style && _style.keywords6 && _style.keywords6.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords6] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords6BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords6TextColor,
                                                 NSFontAttributeName:_style.keywords6Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords6LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords6LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords6UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords6DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式7
        if(_style.hasKeywords7Style && _style.keywords7 && _style.keywords7.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords7] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords7BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords7TextColor,
                                                 NSFontAttributeName:_style.keywords7Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords7LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords7LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords7UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords7DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式8
        if(_style.hasKeywords8Style && _style.keywords8 && _style.keywords8.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords8] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords8BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords8TextColor,
                                                 NSFontAttributeName:_style.keywords8Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords8LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords8LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords8UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords8DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //关键字样式9
        if(_style.hasKeywords9Style && _style.keywords9 && _style.keywords9.length>0)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionFromArray:_style.keywords9] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.keywords9BgColor,
                                                 NSForegroundColorAttributeName:_style.keywords9TextColor,
                                                 NSFontAttributeName:_style.keywords9Font,
                                                 NSStrikethroughColorAttributeName:_style.keywords9LineColor,
                                                 NSUnderlineColorAttributeName:_style.keywords9LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.keywords9UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.keywords9DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //日期
        if(_style.hasDateStyle)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:kExpressionDate inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.dateBgColor,
                                                 NSForegroundColorAttributeName:_style.dateTextColor,
                                                 NSFontAttributeName:_style.dateFont,
                                                 NSStrikethroughColorAttributeName:_style.dateLineColor,
                                                 NSUnderlineColorAttributeName:_style.dateLineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.dateUnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.dateDeleteLine),
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //时间
        if(_style.hasTimeStyle)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:kExpressionTime inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.timeBgColor,
                                                 NSForegroundColorAttributeName:_style.timeTextColor,
                                                 NSFontAttributeName:_style.timeFont,
                                                 NSStrikethroughColorAttributeName:_style.timeLineColor,
                                                 NSUnderlineColorAttributeName:_style.timeLineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.timeUnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.timeDeleteLine),
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //范围样式
        if(_style.hasRangeStyle && _style.rangeStartSymbol && _style.rangeEndSymbol)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionWithStart:_style.rangeStartSymbol andEnd:_style.rangeEndSymbol] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.rangeBgColor,
                                                 NSForegroundColorAttributeName:_style.rangeTextColor,
                                                 NSFontAttributeName:_style.rangeFont,
                                                 NSStrikethroughColorAttributeName:_style.rangeLineColor,
                                                 NSUnderlineColorAttributeName:_style.rangeLineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.rangeUnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.rangeDeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //范围样式2
        if(_style.hasRange2Style && _style.range2StartSymbol && _style.range2EndSymbol)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionWithStart:_style.range2StartSymbol andEnd:_style.range2EndSymbol] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.range2BgColor,
                                                 NSForegroundColorAttributeName:_style.range2TextColor,
                                                 NSFontAttributeName:_style.range2Font,
                                                 NSStrikethroughColorAttributeName:_style.range2LineColor,
                                                 NSUnderlineColorAttributeName:_style.range2LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.range2UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.range2DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }
        
        
        //范围样式3
        if(_style.hasRange3Style && _style.range3StartSymbol && _style.range3EndSymbol)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:[KATExpressionUtil expressionWithStart:_style.range3StartSymbol andEnd:_style.range3EndSymbol] inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.range3BgColor,
                                                 NSForegroundColorAttributeName:_style.range3TextColor,
                                                 NSFontAttributeName:_style.range3Font,
                                                 NSStrikethroughColorAttributeName:_style.range3LineColor,
                                                 NSUnderlineColorAttributeName:_style.range3LineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.range3UnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.range3DeleteLine)
                                                 }
                                         range:range];
                }
            }
        }

        
        
        //电话
        if(_style.hasPhoneStyle)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:kExpressionPhone inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.phoneBgColor,
                                                 NSForegroundColorAttributeName:_style.phoneTextColor,
                                                 NSFontAttributeName:_style.phoneFont,
                                                 NSStrikethroughColorAttributeName:_style.phoneLineColor,
                                                 NSUnderlineColorAttributeName:_style.phoneLineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.phoneUnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.phoneDeleteLine)
                                                 }
                                         range:range];
                }
                
            }
        }
        
        //URL
        if(_style.hasUrlStyle)
        {
            //获取所有的范围
            KATArray *ranges=[KATExpressionUtil rangesWithExpression:kExpressionUrl inString:_text];
            
            if(ranges && ranges.length>0)
            {
                for(int i=0;i<ranges.length;i++)
                {
                    NSRange range=[(NSValue *)[ranges get:i] rangeValue];
                    
                    [_attrString addAttributes:@{
                                                 NSBackgroundColorAttributeName:_style.urlBgColor,
                                                 NSForegroundColorAttributeName:_style.urlTextColor,
                                                 NSFontAttributeName:_style.urlFont,
                                                 NSStrikethroughColorAttributeName:_style.urlLineColor,
                                                 NSUnderlineColorAttributeName:_style.urlLineColor,
                                                 NSUnderlineStyleAttributeName:@(_style.urlUnderLine),
                                                 NSStrikethroughStyleAttributeName:@(_style.urlDeleteLine)
                                                 }
                                         range:range];
                }
                
            }
        }
        
        
        _label.attributedText=_attrString;
        
        
        //计算尺寸
        CGRect rect=[_label.attributedText boundingRectWithSize:CGSizeMake(_label.bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
        
//        self.contentSize=CGSizeMake(self.frame.size.width, rect.size.height+_style.marginTop+_style.marginBottom>self.frame.size.height?rect.size.height+_style.marginTop+_style.marginBottom:self.frame.size.height);
        
        self.contentSize=CGSizeMake(self.frame.size.width, rect.size.height+_style.marginTop+_style.marginBottom);
        self.textSize=rect.size;
    }
    else
    {
        if(!_text)
        {
            self.text=@"";
        }
        
        if(!_style)
        {
            self.style=[KATTextStyle textStyle];
        }
    }
}



//调整显示大小(宽度不变)
- (void)adjustFrame
{
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.contentSize.height);
//    _label.frame=CGRectMake(_label.frame.origin.x, _label.frame.origin.y, _label.frame.size.width, self.contentSize.height-_style.marginTop-_style.marginBottom);
}


//调整显示大小
- (void)resizeFrame
{
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, self.textSize.width+_style.marginLeft+_style.marginRight, self.textSize.height+_style.marginTop+_style.marginBottom);
//    _label.frame=CGRectMake(_label.frame.origin.x, _label.frame.origin.y, self.textSize.width, self.textSize.height);
}


//获取label按钮
- (KATLabelButton *)buttonWithAction:(void (^)(void))action;
{
    KATLabelButton *button=[KATLabelButton buttonWithLabel:_label andAction:action];
    button.frame=self.frame;
    
    return button;
}



- (void)dealloc
{
    [_label release];
    [_text release];
    [_style release];
    [_attrString release];
    
    [super dealloc];
}



@end
